package com.RuiJi.controller;

import com.RuiJi.entity.Employee;
import com.RuiJi.service.EmployeeService;
import com.RuiJi.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author Twinkle
 * @PACKAGE_NAME: com.RuiJi.controller
 * @NAME: EmployeeController
 * @USER: Twinkle
 * @DATE: 2023/4/1
 * @TIME: 17:07
 * @YEAR: 2023
 * @MONTH: 04
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        //页面密码MD5加密
        String password = employee.getPassword();
        password = SecureUtil.md5().digestHex(password.getBytes());
        //查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        //没有查询到返回结果
        if (one == null) {
            return R.error("登录失败了哦");
        }
        if (!one.getPassword().equals(password)) {
            return R.error("密码错了喔");
        }
        if (one.getStatus() == 0) {
            return R.error("账号存在异常的嘞");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {

        request.getSession().removeAttribute("employee");
        return R.success("欢迎下次光临的呢");
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工信息{}" + employee.toString());
        employee.setPassword(SecureUtil.md5().digestHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        ////获得当前登录用户id
        //Long employeeId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(employeeId);
        //employee.setUpdateUser(employeeId);
        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 员工信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件

        if (name != null) {
            queryWrapper.like(Employee::getName, name);
        }
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        Long emp = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(emp);
        employeeService.updateById(employee);
        return R.success("成功了");
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("修改员工{}",id);
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }


}
